//package com.smartboot.socket.decoder;
//
//import org.smartboot.socket.Protocol;
//import org.smartboot.socket.buffer.VirtualBuffer;
//import org.smartboot.socket.extension.decoder.DelimiterFrameDecoder;
//import org.smartboot.socket.transport.AioSession;
//
//import java.nio.ByteBuffer;
//
///**
// * @author 三刀
// * @version V1.0 , 2018/4/26
// */
//public class DelimiterProtocol implements Protocol<String> {
//
//    //结束符\r\n
//    private static final byte[] DELIMITER_BYTES = new byte[]{'\r', '\n'};
//
//    @Override
//    public String decode(ByteBuffer buffer, AioSession<String> session) {
//        DelimiterFrameDecoder delimiterFrameDecoder;
//        if (session.getAttachment() == null) {//构造指定结束符的临时缓冲区
//            delimiterFrameDecoder = new DelimiterFrameDecoder(DELIMITER_BYTES, 64);
//            session.setAttachment(delimiterFrameDecoder);//缓存解码器已应对半包情况
//        } else {
//            delimiterFrameDecoder = session.getAttachment();
//        }
//
//        //未解析到DELIMITER_BYTES则返回null
//        if (!delimiterFrameDecoder.decode(buffer)) {
//            return null;
//        }
//        //解码成功
//        ByteBuffer byteBuffer = delimiterFrameDecoder.getBuffer();
//        byte[] bytes = new byte[byteBuffer.remaining()];
//        byteBuffer.get(bytes);
//        session.setAttachment(null);//释放临时缓冲区
//        return new String(bytes);
//    }
//
//    @Override
//    public VirtualBuffer encode(String msg, AioSession<String> session) {
//        byte[] bytes = msg.getBytes();
//        ByteBuffer buffer = ByteBuffer.allocate(bytes.length + DELIMITER_BYTES.length);
//        buffer.put(bytes).put(DELIMITER_BYTES);
//        buffer.flip();
//        return null;
//    }
//}
